﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ApplicationDllManager
{
    public partial class frmProcessList : Form
    {
        public frmProcessList()
        {
            InitializeComponent();
        }

        public delegate void OnProcessSelectDelegate(OnProcessSelectEventArgs e);
        public event OnProcessSelectDelegate OnProcessSelect;

        private void frmProcessList_Load(object sender, EventArgs e)
        {
            Process[] processes = Process.GetProcesses();

            listbProcesses.Items.Clear();
            foreach (Process p in processes)
            {
                listbProcesses.Items.Add(p.ProcessName);
            }
            listbProcesses.Sorted = true;
            lblProcessCount.Text = "Process count: " + processes.Length;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (listbProcesses.SelectedIndex >= 0)
            {
                OnProcessSelectEventArgs args = new OnProcessSelectEventArgs(listbProcesses.SelectedItem.ToString());
                OnProcessSelect(args);
            }
            
            this.Close();
        }

        private void btnRefreshProcessList_Click(object sender, EventArgs e)
        {
            frmProcessList_Load(null, null);
        }
    }

    public class OnProcessSelectEventArgs : EventArgs
    {
        string process_name;

        public OnProcessSelectEventArgs(string ProcessName)
        {
            this.process_name = ProcessName;
        }

        public string ProcessName
        {
            get { return process_name; }
        }
    }
}
